package net.common.hibernate;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.common.service.ArgumentBean;
import net.common.util.StringUtils;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

public class CriteriaHandler {
	
	/**
	 * 构造条件
	 * 
	 * @param criteria
	 * @param argumentsBeans
	 */
	public static void createCriteria(Criteria criteria, List<ArgumentBean> argumentsBeans) {
		if (!ObjectUtils.isNull(argumentsBeans)) {
			// isOr“or”条件
			boolean isOr = false;
			List<Criterion> criterions = null;
			for (ArgumentBean argumentsBean : argumentsBeans) {
				List<String> vals = argumentsBean.getValues();
				if (vals == null || vals.size() == 0 || vals.get(0).equals("")) {
					continue;
				}
				// “and”和“or”处理，自定义查询条件规则
				if (!argumentsBean.isAnd()) {
					// 如果where条件连接符是OR
					isOr = true;
					criterions = new ArrayList<Criterion>();
					criterions.add(handlerArguments(argumentsBean));
					continue;
				} else {
					// 如果where条件连接符不是OR
					criteria.add(handlerArguments(argumentsBean));
					// 如果isOr为flase，情况orArgumentsBean
					isOr = false;
				}
				// 如果isOr为flase，并且orArgumentsBean不为NULL
				if (!isOr && criterions != null && criterions.size() > 0) {
					CustomerLogicalExpression customerLogicalExpression = new CustomerLogicalExpression(criterions,
							"or");
					criteria.add(customerLogicalExpression);
					criterions = null;
				}
			}
		}
	}

	@SuppressWarnings("rawtypes")
	private static Criterion handlerArguments(ArgumentBean arguments) {
		try {
			if (StringUtils.equalsign(arguments.getSymbol(), Symbol.eq.getValue())) {
				Object value = ObjectUtils.fromStringToObject(arguments.getParamType(), arguments.getValues()
						.get(0));
				return Restrictions.eq(arguments.getParamName(), value);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.between.getValue())) {
				Object first = ObjectUtils.fromStringToObject(arguments.getParamType(), arguments.getValues()
						.get(0));
				Object end = ObjectUtils.fromStringToObject(arguments.getParamType(), arguments.getValues()
						.get(1));
				return Restrictions.between(arguments.getParamName(), first, end);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.gt.getValue())) {
				Object value = ObjectUtils.fromStringToObject(arguments.getParamType(), arguments.getValues()
						.get(0));
				return Restrictions.gt(arguments.getParamName(), value);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.ge.getValue())) {
				Object value = ObjectUtils.fromStringToObject(arguments.getParamType(), arguments.getValues()
						.get(0));
				return Restrictions.ge(arguments.getParamName(), value);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.lt.getValue())) {
				Object value = ObjectUtils.fromStringToObject(arguments.getParamType(), arguments.getValues()
						.get(0));
				return Restrictions.lt(arguments.getParamName(), value);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.in.getValue())) {
				Collection value = ObjectUtils.fromStringArrayToCollection(arguments.getParamType(), arguments
						.getValues());
				return Restrictions.in(arguments.getParamName(), value);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.eqProperty.getValue())) {
				String value = arguments.getValues().get(0);
				return Restrictions.eqProperty(arguments.getParamName(), value);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.gtProperty.getValue())) {
				String value = arguments.getValues().get(0);
				return Restrictions.gtProperty(arguments.getParamName(), value);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.geProperty.getValue())) {
				String value = arguments.getValues().get(0);
				return Restrictions.geProperty(arguments.getParamName(), value);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.ltProperty.getValue())) {
				String value = arguments.getValues().get(0);
				return Restrictions.ltProperty(arguments.getParamName(), value);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.leProperty.getValue())) {
				String value = arguments.getValues().get(0);
				return Restrictions.leProperty(arguments.getParamName(), value);
			} else if (StringUtils.equalsign(arguments.getSymbol(), Symbol.sql.getValue())) {
				String value = arguments.getParamName();
				return Restrictions.sqlRestriction(value);
			}
		} catch (ParseException e) {
			throw new HibernateQueryException("日期解析", e);
		} catch (Exception e) {
			throw new HibernateQueryException("", e);
		}
		throw new HibernateQueryException("Hibernate查询条件语句中存在问题");
	}
}
